<template>
	<div class="pdf-viewer">
		<!-- 顶部导航栏 -->
		<div class="pdf-header">
			<el-button type="primary" @click="goBack" class="back-btn">
				<el-icon><ArrowLeft /></el-icon>
				返回
			</el-button>
			<div class="pdf-title">
				<h3>{{ pdfTitle }}</h3>
			</div>
			<div class="header-actions">
				<el-button @click="downloadPdf" type="success">
					<el-icon><Download /></el-icon>
					下载
				</el-button>
			</div>
		</div>

		<!-- PDF显示区域 -->
		<div class="pdf-container">
			<iframe
				:src="pdfUrl"
				width="100%"
				height="100%"
				frameborder="0"
				class="pdf-frame"
				@load="onPdfLoad"
				@error="onPdfError"
			></iframe>
		</div>

		<!-- 加载状态 -->
		<div v-if="loading" class="loading-overlay">
			<el-loading size="large" text="正在加载PDF文件..." />
		</div>
	</div>
</template>

<script setup>
import { ref, onMounted, computed } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElLoading } from "element-plus";
import { ArrowLeft, Download } from "@element-plus/icons-vue";
import { processPdfUrl } from "@/utils/pdfUrlHandler.js";

const router = useRouter();
const route = useRoute();

// 响应式数据
const loading = ref(true);
const pdfUrl = ref("");
const pdfTitle = ref("");

// 计算属性
const currentPdfUrl = computed(() => {
	return route.query.url || "";
});

const currentTitle = computed(() => {
	return route.query.title || "PDF文档";
});

// 方法
function goBack() {
	router.back();
}

function downloadPdf() {
	if (pdfUrl.value) {
		const link = document.createElement("a");
		link.href = pdfUrl.value;
		link.download = pdfTitle.value + ".pdf";
		link.target = "_blank";
		document.body.appendChild(link);
		link.click();
		document.body.removeChild(link);
		ElMessage.success("开始下载PDF文件");
	} else {
		ElMessage.warning("无法下载，PDF URL为空");
	}
}

function onPdfLoad() {
	loading.value = false;
	console.log("PDF加载完成");
}

function onPdfError() {
	loading.value = false;
	ElMessage.error("PDF文件加载失败，请检查文件路径");
	console.error("PDF加载失败，URL:", pdfUrl.value);
}

// 生命周期
onMounted(() => {
	// 从路由参数获取PDF URL和标题，并进行URL转换
	const originalUrl = currentPdfUrl.value;
	pdfUrl.value = processPdfUrl(originalUrl);
	pdfTitle.value = currentTitle.value;

	console.log("PDF查看器初始化完成");
	console.log("原始PDF URL:", originalUrl);
	console.log("转换后PDF URL:", pdfUrl.value);
	console.log("PDF标题:", pdfTitle.value);

	// 如果URL为空，显示错误信息
	if (!pdfUrl.value) {
		ElMessage.error("未提供PDF文件路径");
		loading.value = false;
	}
});
</script>

<style scoped>
.pdf-viewer {
	height: 100vh;
	display: flex;
	flex-direction: column;
	background-color: #f5f5f5;
}

.pdf-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 16px 24px;
	background-color: #fff;
	box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	border-bottom: 1px solid #e4e7ed;
	flex-shrink: 0;
}

.back-btn {
	margin-right: 16px;
}

.pdf-title {
	flex: 1;
	text-align: center;
}

.pdf-title h3 {
	margin: 0;
	color: #303133;
	font-size: 18px;
	font-weight: 600;
}

.header-actions {
	display: flex;
	gap: 12px;
}

.pdf-container {
	flex: 1;
	position: relative;
	background-color: #fff;
	margin: 16px;
	border-radius: 8px;
	box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
	overflow: hidden;
}

.pdf-frame {
	border: none;
}

.loading-overlay {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: rgba(255, 255, 255, 0.9);
	display: flex;
	align-items: center;
	justify-content: center;
	z-index: 1000;
}

/* 响应式设计 */
@media (max-width: 768px) {
	.pdf-header {
		padding: 12px 16px;
		flex-wrap: wrap;
		gap: 8px;
	}

	.pdf-title {
		order: 2;
		flex-basis: 100%;
		margin-top: 8px;
	}

	.pdf-title h3 {
		font-size: 16px;
		text-align: center;
	}

	.header-actions {
		order: 1;
	}

	.back-btn {
		order: 0;
		margin-right: 0;
	}

	.pdf-container {
		margin: 8px;
		border-radius: 4px;
	}

	/* 移动端按钮样式优化 */
	:deep(.el-button) {
		font-size: 14px;
		padding: 8px 12px;
	}

	/* 移动端加载状态优化 */
	.loading-overlay {
		font-size: 14px;
	}
}

/* 超小屏幕手机 (320px - 480px) */
@media (max-width: 480px) {
	.pdf-header {
		padding: 8px 12px;
	}

	.pdf-title h3 {
		font-size: 14px;
	}

	.pdf-container {
		margin: 4px;
		border-radius: 2px;
	}

	:deep(.el-button) {
		font-size: 12px;
		padding: 6px 10px;
	}

	/* 移动端按钮图标大小调整 */
	:deep(.el-button .el-icon) {
		font-size: 14px;
	}
}

/* 平板设备适配 (769px - 1024px) */
@media (min-width: 769px) and (max-width: 1024px) {
	.pdf-header {
		padding: 14px 20px;
	}

	.pdf-container {
		margin: 12px;
	}
}
</style>
